<?php
/**
 * Add/edit gallery
 */
include ("configs/config.php");



if ( $_SESSION['LOGIN_NAME'] <> "admin" ) 
	{
	header("Location: login-admin.php");
	exit();
	}




$tpl = new XTemplate("./template/tpl_admin.html");
$tpl->assign('server_path',$SERVER_PATH);
$tpl->parse("main.body.menu");

$query=("select * from a_gallery;");
$result=mysql_query($query);

$error=0;

if ($_GET["clicked"]==1)
{
	$name=addslashes($_GET["name"]);
	$id=$_GET["id"];
	$description = addslashes($_GET['description']);
	if ($name=="")
	{
		$error++;
		$msg_error.="Please enter a name for this category</br>";
	}
	else
	{	
		$query='update a_gallery set name="'.$name.'", description="'.$description.'" where id='.$id.';';
	
		$result=mysql_query($query);
		header("Location: edit-gallery.php");
		exit();
		}
}	

if ($_GET['delete']<>0)
{
	$id=$_GET['delete'];
	$query=("delete  from a_gallery where id=$id");
	$result=mysql_query($query);
	
	if (mysql_affected_rows()<1)
		{
			$error++;
			$msg_error.="There was a problem with the database! Please come back in 10 minutes";
		}	
	else
	{
		header("Location: edit-gallery.php");
		exit();
	}
}


if ($_GET['add_clicked'])
{
	if ($_GET['name']<>"")
		{
			$name=addslashes($_GET['name']);
			$description=addslashes($_GET['description']);
			$query=("insert into a_gallery (name,description) values('$name','$description');");
			mysql_query($query);
			if (mysql_affected_rows()<1)
			{
					$error++;
					$msg_error="There was a problem with the database! Please come back in 10 minutes";
				
			}
			else
			{
				header("Location: edit-gallery.php");
				exit();
			}
		}
		else
		{
			$error++;
			$msg_error.="-please enter a name for this gallery</br>";
		}
}



$query=("select * from a_gallery order by name ;");
$result=mysql_query($query);

if (mysql_num_rows($result)<1)
{
	$msg_error="There was a problem with the database! Please come back in 10 minutes";
	$error++;
}	
else	
{

	while ($group=mysql_fetch_assoc($result))
	{
		$tpl->assign($group);
		$tpl->assign("msg_error",$msg_error);
		$tpl->parse("main.body.edit_gallery.row");
		
	};

}


$tpl->assign("error",$msg_error);
$tpl->parse("main.body.edit_gallery.error");
$tpl->parse("main.body.edit_gallery");
$tpl->parse("main.body");

$tpl->parse("main");
$tpl->out("main");
?>